<Project>
  <PropertyGroup>
    <!--
      Set ChromeChannel properties to get the latest versions
    -->
    <ChromeChannel>stable</ChromeChannel>

    <ChromeOSIdentifier Condition="$([MSBuild]::IsOSPlatform('windows'))">Windows</ChromeOSIdentifier>
    <ChromeOSIdentifier Condition="$([MSBuild]::IsOSPlatform('linux'))">Linux</ChromeOSIdentifier>
    <ChromeOSIdentifier Condition="'$(ChromeOSIdentifier)' == ''">unsupported-platform</ChromeOSIdentifier>

    <!-- disable by default on unsupported platforms -->
    <InstallChromeForTests Condition="'$(InstallChromeForTests)' == '' and '$(ChromeOSIdentifier)' == ''">false</InstallChromeForTests>
    <InstallV8ForTests Condition="'$(InstallV8ForTests)' == ''">false</InstallV8ForTests>

    <FirefoxDir>$(ArtifactsBinDir)firefox\</FirefoxDir>
    <FirefoxStampFile>$([MSBuild]::NormalizePath($(FirefoxDir), '.install-firefox-$(FirefoxRevision).stamp'))</FirefoxStampFile>
    <_BrowserStampDir>$(ArtifactsBinDir)\</_BrowserStampDir>

    <WasmProvisionAfterTarget Condition="'$(WasmProvisionAfterTarget)' == ''">Build</WasmProvisionAfterTarget>
  </PropertyGroup>

  <Import Project="$(MSBuildThisFileDirectory)ChromeVersions.props" />

  <PropertyGroup Condition="'$(BrowserHost)' != 'windows'">
    <FirefoxRevision>108.0.1</FirefoxRevision>
    <FirefoxUrl>https://ftp.mozilla.org/pub/firefox/releases/$(FirefoxRevision)/linux-x86_64/en-US/firefox-$(FirefoxRevision).tar.bz2</FirefoxUrl>
    <FirefoxBinaryName>firefox</FirefoxBinaryName>
  </PropertyGroup>

  <PropertyGroup Condition="$([MSBuild]::IsOSPlatform('linux'))">
    <ChromeDirName>chrome-linux</ChromeDirName>
    <ChromeDriverDirName>chromedriver_linux64</ChromeDriverDirName>
    <ChromeBinaryName>chrome</ChromeBinaryName>
    <ChromeDriverBinaryName>chromedriver</ChromeDriverBinaryName>
    <_ChromeOSPrefix>Linux_x64</_ChromeOSPrefix>

    <ChromeVersion>$(linux_ChromeVersion)</ChromeVersion>
    <ChromeRevision>$(linux_ChromeRevision)</ChromeRevision>
    <_ChromeBaseSnapshotUrl>$(linux_ChromeBaseSnapshotUrl)</_ChromeBaseSnapshotUrl>

    <ChromeUrl>$(linux_ChromeBaseSnapshotUrl)/chrome-linux.zip</ChromeUrl>
    <ChromeDriverUrl>$(linux_ChromeBaseSnapshotUrl)/chromedriver_linux64.zip</ChromeDriverUrl>

    <V8Version>$(linux_V8Version)</V8Version>
    <V8DirName>v8-$(linux_V8Version)</V8DirName>
    <V8BinaryName>$(V8DirName).sh</V8BinaryName>
    <_V8PlatformId>linux64</_V8PlatformId>
  </PropertyGroup>

  <PropertyGroup Condition="$([MSBuild]::IsOSPlatform('windows'))">
    <ChromeDirName>chrome-win</ChromeDirName>
    <ChromeDriverDirName>chromedriver_win32</ChromeDriverDirName>
    <ChromeBinaryName>chrome.exe</ChromeBinaryName>
    <ChromeDriverBinaryName>chromedriver.exe</ChromeDriverBinaryName>
    <_ChromeOSPrefix>Win_x64</_ChromeOSPrefix>

    <ChromeVersion>$(win_ChromeVersion)</ChromeVersion>
    <ChromeRevision>$(win_ChromeRevision)</ChromeRevision>
    <_ChromeBaseSnapshotUrl>$(win_ChromeBaseSnapshotUrl)</_ChromeBaseSnapshotUrl>

    <ChromeUrl>$(win_ChromeBaseSnapshotUrl)/chrome-win.zip</ChromeUrl>
    <ChromeDriverUrl>$(win_ChromeBaseSnapshotUrl)/chromedriver_win32.zip</ChromeDriverUrl>

    <V8Version>$(win_V8Version)</V8Version>
    <V8DirName>v8-$(win_V8Version)</V8DirName>
    <V8BinaryName>$(V8DirName).cmd</V8BinaryName>
    <_V8PlatformId>win32</_V8PlatformId>
  </PropertyGroup>

  <PropertyGroup Condition="'$(ChromeVersion)' != ''">
    <ChromeDir>$(ArtifactsBinDir)chrome\</ChromeDir>
    <ChromeDriverDir>$(ArtifactsBinDir)chromedriver\</ChromeDriverDir>
    <ChromeStampFile>$([MSBuild]::NormalizePath('$(ChromeDir)', '.install-$(ChromeVersion)-$(ChromeRevision).stamp'))</ChromeStampFile>
    <ChromeDriverStampFile>$([MSBuild]::NormalizePath('$(ChromeDriverDir)', '.install-$(ChromeVersion)-$(ChromeRevision).stamp'))</ChromeDriverStampFile>

    <ChromeBinaryPath>$([MSBuild]::NormalizePath($(ChromeDir), $(ChromeDirName), $(ChromeBinaryName)))</ChromeBinaryPath>
    <ChromeDriverBinaryPath>$([MSBuild]::NormalizePath($(ChromeDriverDir), $(ChromeDriverDirName), $(ChromeDriverBinaryName)))</ChromeDriverBinaryPath>
  </PropertyGroup>

  <PropertyGroup Condition="'$(V8Version)' != ''">
    <V8Dir>$(ArtifactsBinDir)$(V8DirName)\</V8Dir>
    <V8StampFile>$([MSBuild]::NormalizePath('$(V8Dir)', '.install-$(V8Version).stamp'))</V8StampFile>
    <V8BinaryPath>$([MSBuild]::NormalizePath($(V8Dir), $(V8BinaryName)))</V8BinaryPath>
  </PropertyGroup>

  <PropertyGroup Condition="'$(BrowserHost)' != 'windows'">
    <FirefoxRevision>108.0.1</FirefoxRevision>
    <FirefoxUrl>https://ftp.mozilla.org/pub/firefox/releases/$(FirefoxRevision)/linux-x86_64/en-US/firefox-$(FirefoxRevision).tar.bz2</FirefoxUrl>
    <FirefoxBinaryName>firefox</FirefoxBinaryName>
  </PropertyGroup>

  <Target Name="DownloadAndInstallChrome"
          AfterTargets="$(WasmProvisionAfterTarget)"
          Condition="(!Exists($(ChromeStampFile)) or !Exists($(ChromeBinaryPath))) and '$(InstallChromeForTests)' == 'true'">

    <Error Condition="!$([MSBuild]::IsOSPlatform('linux')) and !$([MSBuild]::IsOSPlatform('windows'))"
           Text="Chrome provisioning only supported on Linux, and windows." />
    <Error Condition="'$(ChromeVersion)' == ''"
           Text="No %24(ChromeVersion) set. This can be set in eng/testing/ChromeVersions.props" />

    <RemoveDir Directories="$(ChromeDir)" />

    <Message Text="** Installing chrome version $(ChromeVersion), rev: $(ChromeRevision). To disable this set the msuild property InstallChromeForTests=false ."
             Importance="High" />

    <DownloadFile SourceUrl="$(ChromeUrl)" DestinationFolder="$(ArtifactsObjDir)" SkipUnchangedFiles="false">
      <Output TaskParameter="DownloadedFile" PropertyName="_DownloadedFile" />
    </DownloadFile>
    <Unzip SourceFiles="$(_DownloadedFile)" DestinationFolder="$(ChromeDir)" />
    <Delete Files="$(_DownloadedFile)" />

    <Error Text="Cannot find chrome at $(ChromeBinaryPath) in the downloaded copy"
           Condition="!Exists($(ChromeBinaryPath))" />

    <Exec Command="chmod +x $(ChromeBinaryPath)" Condition="!$([MSBuild]::IsOSPlatform('windows'))" />

    <Touch Files="$(ChromeStampFile)" AlwaysCreate="true" />
  </Target>

  <Target Name="DownloadAndInstallChromeDriver"
          AfterTargets="$(WasmProvisionAfterTarget)"
          Condition="(!Exists($(ChromeDriverStampFile)) or !Exists($(ChromeDriverBinaryPath))) and '$(InstallChromeForTests)' == 'true'">

    <Error Condition="!$([MSBuild]::IsOSPlatform('linux')) and !$([MSBuild]::IsOSPlatform('windows'))"
           Text="ChromeDriver provisioning only supported on Linux, and windows." />
    <Error Condition="'$(ChromeVersion)' == ''"
           Text="No %24(ChromeVersion) set. This can be set in eng/testing/ChromeVersions.props" />

    <RemoveDir Directories="$(ChromeDriverDir)" />

    <Message Text="** Installing chromedriver version $(ChromeVersion), rev: $(ChromeRevision). To disable this set the msuild property InstallChromeForTests=false ."
             Importance="High" />

    <DownloadFile SourceUrl="$(ChromeDriverUrl)" DestinationFolder="$(ArtifactsObjDir)" SkipUnchangedFiles="false">
      <Output TaskParameter="DownloadedFile" PropertyName="_DownloadedFile" />
    </DownloadFile>
    <Unzip SourceFiles="$(_DownloadedFile)" DestinationFolder="$(ChromeDriverDir)" />

    <Error Text="Cannot find chromedriver at $(ChromeDriverBinaryPath) in the downloaded copy"
           Condition="!Exists($(ChromeDriverBinaryPath))" />

    <Exec Command="chmod +x $(ChromeDriverBinaryPath)" Condition="!$([MSBuild]::IsOSPlatform('windows'))" />

    <Touch Files="$(ChromeDriverStampFile)" AlwaysCreate="true" />
  </Target>

  <Target Name="DownloadAndInstallV8"
          AfterTargets="$(WasmProvisionAfterTarget)"
          Condition="(!Exists($(V8StampFile)) or !Exists($(V8BinaryPath))) and '$(InstallV8ForTests)' == 'true'">

    <Error Condition="!$([MSBuild]::IsOSPlatform('linux')) and !$([MSBuild]::IsOSPlatform('windows'))"
           Text="V8 provisioning only supported on Linux, and windows." />
    <Error Condition="'$(V8Version)' == ''" Text="%24(V8Version) not set" />
    <Error Condition="'$(_V8PlatformId)' == ''" Text="%24(_V8PlatformId) not set, needed for constructing the snapshot url." />

    <PropertyGroup>
      <_V8SnapshotUrl>https://storage.googleapis.com/chromium-v8/official/canary/v8-$(_V8PlatformId)-rel-$(V8Version).zip</_V8SnapshotUrl>

      <_V8Script Condition="$([MSBuild]::IsOSPlatform('linux'))">#!/usr/bin/env bash
export __SCRIPT_DIR=%24( cd -- "%24( dirname -- "%24{BASH_SOURCE[0]}" )" &amp;> /dev/null &amp;&amp; pwd )
"$__SCRIPT_DIR/d8" --snapshot_blob="$__SCRIPT_DIR/snapshot_blob.bin" "$@"
      </_V8Script>
      <_V8Script Condition="$([MSBuild]::IsOSPlatform('windows'))">@echo off
"%~dp0\d8.exe --snapshot_blob="%~dp0\snapshot_blob.bin" %*
      </_V8Script>
    </PropertyGroup>

    <DownloadFile SourceUrl="$(_V8SnapshotUrl)" DestinationFolder="$(V8Dir)" SkipUnchangedFiles="true">
      <Output TaskParameter="DownloadedFile" PropertyName="_DownloadedFile" />
    </DownloadFile>

    <Unzip SourceFiles="$(_DownloadedFile)" DestinationFolder="$(V8Dir)" />

    <WriteLinesToFile Lines="$(_V8Script)" File="$(V8BinaryPath)" Overwrite="true" />
    <Exec Command="chmod +x $(V8BinaryPath) $(V8Dir)/d8" Condition="$([MSBuild]::IsOSPlatform('linux'))" />

    <Touch Files="$(V8StampFile)" AlwaysCreate="true" />
  </Target>

  <Target Name="DownloadAndInstallFirefox"
          AfterTargets="$(WasmProvisionAfterTarget)"
          Condition="!Exists($(FirefoxStampFile)) and '$(InstallFirefoxForTests)' == 'true' and !$([MSBuild]::IsOSPlatform('windows'))">
    <ItemGroup>
      <_StampFile Include="$(_BrowserStampDir).install-firefox*.stamp" />
    </ItemGroup>

    <Delete Files="@(_StampFile)" />
    <RemoveDir Directories="$(FirefoxDir)" />

    <DownloadFile SourceUrl="$(FirefoxUrl)" DestinationFolder="$(FirefoxDir)" SkipUnchangedFiles="true">
      <Output TaskParameter="DownloadedFile" PropertyName="_DownloadedFile" />
    </DownloadFile>
    <Exec Command="tar -xf $(_DownloadedFile) -C $(FirefoxDir)"/>
    <Exec Command="rm -rf $(_DownloadedFile)"/>

    <PropertyGroup>
      <_FirefoxBinaryPath>$([MSBuild]::NormalizePath($(FirefoxDir), $(FirefoxBinaryName)))</_FirefoxBinaryPath>
    </PropertyGroup>

    <Error Text="Cannot find firefox at $(_FirefoxBinaryPath) in the downloaded copy"
           Condition="!Exists($(_FirefoxBinaryPath))" />

    <Exec Command="chmod +x $(_FirefoxBinaryPath)"/>

    <Touch Files="$(FirefoxStampFile)" AlwaysCreate="true" />
  </Target>
</Project>
